package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegionQry;
import com.iwhalecloud.bss.kite.cucc.service.attr.UnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalZxCityAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@AttrHandler(LocalZxCityAttrHandler.class)
public class LocalZxCityAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private UnicomRegionService unicomRegionService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String areaFieldName = "";
        if(KiteStringUtils.equals(attr.getFieldName(), "2_0000221")){
            areaFieldName = "2_0000222";
        }
        else if (KiteStringUtils.equals(attr.getFieldName(), "2_0000109")){
            areaFieldName = "2_0000111";
        }
        if (KiteStringUtils.isEmpty(areaFieldName)){
            return;
        }

        KiteAttr areaAttr = inst.getAttr(areaFieldName);
        if(Objects.isNull(areaAttr)){
            return;
        }
        if (KiteStringUtils.isNotEmpty(attr.getValue())) {
            List<UnicomRegion> unicomRegions = unicomRegionService.subRegionFromPlatform(new UnicomRegionQry(attr.getValue(), "30", inst.getSpecId()));
            if (KiteListUtils.isEmpty(unicomRegions)) {
                return;
            }
            List<KiteAttrValue> kiteAttrValues = unicomRegions.stream().map(unicomRegion -> {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(unicomRegion.getRegionCode());
                kiteAttrValue.setAttrValueName(unicomRegion.getRegionName());
                return kiteAttrValue;
            }).collect(Collectors.toList());

            operateInstAttrService.setAttrValue(inst, areaAttr.getFieldName(), "", data.getSceneInstId(), null);
            areaAttr.setAttrValues(kiteAttrValues);
        }
        else {
            areaAttr.setAttrValues(Collections.EMPTY_LIST);
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
